<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_MceLinkManagerAction extends m_backoffice_extends_Action
{
	public function execute()
	{
        $targets = $this->getTargets();
        $websitePages = $this->getPages();
        $rlm = f_relation_Manager::getInstance();

        $data = array();

        foreach($websitePages as $websiteId => $langs)
        {
            foreach($langs as $websiteLang => $pages)
            {
                $ws['website'] = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $websiteId, $websiteLang);

                foreach($pages as $page)
                {
                    $page = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $page['document_uid'], $websiteLang);
                    $parent = $page->getDocumentParent();

                    $ws['childs'][$parent->getUniqueId()]['menu'] = $parent;
                    $ws['childs'][$parent->getUniqueId()]['pages'][] = $page;
                }

                ksort($ws['childs']);

                $data[] = $ws;
            }
        }

		$this->addToModel('targets', $targets);
		$this->addToModel('pages', $data);
		$this->addToModel('medias', $this->getMedias());
		$this->setView('success');
	}	
	
    private function getTargets()
    {
        $targets = f_target_Manager::getAssignedTargets('website', 'unique');
        $result = array();

        if(is_array($targets) && count($targets) > 0)
        {
            $temp = array();
            foreach($targets as $target)
            {
                $temp[$target['parent_id']][$target['document_lang']][] = $target;
            }

            foreach($temp as $websiteId => $website)
            {
                foreach($website as $lang => $targets)
                {
                    $res['website'] = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $websiteId, $lang);

                    foreach($targets as $target)
                    {
                        $desc = f_target_Manager::getTargetDescription('website', $target['label']);
                        $t['name'] = $target['label'];
                        $t['label'] = $desc['label'];
                        $t['document'] = $this->getDocumentProvider()->getByUniqueId($target['document_uid'], $target['document_lang']);
                        $res['targets'][] = $t;
                    }

                    $result[] = $res;
                }
            }
        }

        return $result;
    }

    private function getPages()
    {
        $pages = $this->getDocumentProvider()->loadDocuments("modules_website/page")
                                    ->byPublicationStatus("PUBLISHED")                                    
                                    ->addAscOrder("document_label")
                                    ->retrieveColumnValues(array('document_uid', 'document_label', 'document_lang','websiteId'), PDO::FETCH_ASSOC, $this->getContextLang());

        $result = array();

        if(is_array($pages) && count($pages) > 0)
        {
            foreach($pages as $page)
            {
                $result[$page['websiteId']][$page['document_lang']][] = $page;
            }
        }

        return $result;
    }

    private function getMedias()
    {
        return $this->getDocumentProvider()->loadDocuments("modules_medias/media")
                                    ->byPublicationStatus("PUBLISHED")
                                    ->whereIsNotEqual('mime', array('image/jpeg','image/png'))
                                    ->addAscOrder("document_label")
                                    ->retrieveColumnValues(array('document_uid', 'document_label'), PDO::FETCH_ASSOC, $this->getContextLang());
    }
}